package com.hsl.study.exercise.day0106;

import java.util.concurrent.*;

/**
 * 需求使用线程池实现三个售票员卖50张票
 * <p>
 * 线程 操作 资源类
 */

//资源类

public class ThreadPollExecutor {

    private static int count = 50;

    public static void main(String[] args) {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                3,
                0,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.out.println("您的页面起飞了");
                    }
                }
        );
        for (int i = 1; i <= 3; i++) {
            String sum = String.valueOf(i);
            threadPoolExecutor.submit(() -> {
                        while (true) {
                            synchronized (ThreadPollExecutor.class) {
                                if (count > 0) {
                                    System.out.println("售票员" + sum + "卖出了第" + count + "张票");
                                    count--;
                                } else {
                                    break;
                            }
                        }
                    }
        });
        }
        threadPoolExecutor.shutdown();
    }
}

